<?php

namespace app\data\controller;

use app\data\controller\base\Publics;
use think\Db;
use think\Log;

/**
 * 统计 - 汇总
 * Class
 * @package app\data\controller
 */
class Suming
{

	/**
	 * 实例对象
	 * @var object
	 */
	protected static $instance;

	/**
	 * 表
	 * @var  Publics::instance()->tab();
	 */
	protected $tab = null;

	public function __construct()
	{
		$this->tab = Publics::instance()->tab();
	}

	/**
	 * 实例化
	 */
	public static function instance()
	{
		if (is_null(self::$instance)) {

			self::$instance = new static();
		}
		return self::$instance;
	}

	/**
	 * 超标交易
	 */
	public function out_stands()
	{
		return Db::name($this->tab->tran_warn)->count();
	}

	/**
	 * 超标交易
	 */
	public function mon_out_stands()
	{
		return Db::name($this->tab->tran_warn)->whereTime("times", 'month')->count();
	}

	/**
	 * 流失客户
	 */
	public function lost_customer()
	{
		$now =  strtotime(date("Y-m-d", time()));
		$where = "ends < " .  $now;
		$field = "CAST( UNIX_TIMESTAMP( DATE_ADD( FROM_UNIXTIME( last_time, '%Y-%m-%d' ), INTERVAL termDays DAY ) ) AS UNSIGNED ) ends";
		$buildSql = Db::name("trans_lost_customer")->field($field)->buildSql();
		return Db::table($buildSql)->alias('a')->where($where)->count();
	}

	/**
	 * 激活未达标
	 */
	public function active_unstands()
	{
		$defaultWhere = ["stands_typs" => 1, "process" => 2];
		$now =  strtotime(date("Y-m-d", time()));
		$timesWhere = "end_times < " . $now;
		return  Db::name($this->tab->acte_stands)->where($defaultWhere)->where($timesWhere)->count();
	}
}
